var $table = $("#companyTab");
var bindingBankURL = "http://payment.hfpay.net:8080/v1.0/channel/bindPayCard";
$(function () {

	initTable();
    //查询事件
    $("#queryCompanyBtn").click(function () {
        $table.bootstrapTable('refresh');
    });
    //增加公司按钮
    $("#addBut").click(function () {
    	initCompanyEdit(null);
    });
    
    //账户开户
    $("#accountadd").click(function () {
    	var selectContent = $table.bootstrapTable('getSelections')[0];
        if(typeof(selectContent) == 'undefined') {
        	layer.msg('请选择一条数据!');
            return false;
        }else{
        	var comnumId = selectContent.comnumId;
        	var comnumNum = selectContent.comnumNum;
        	var comnumAccountNo = selectContent.comnumAccountNo;
        	var comnumIsDelete = selectContent.comnumIsDelete;
        	if(comnumIsDelete == '1'){
        		layer.msg('禁用状态下不能开户');
        	}else if(comnumAccountNo!= ''&comnumAccountNo!= null&comnumAccountNo!= undefined  ){
        		layer.msg('已开户，无需重复开户');
        	}else{
        		var index = layer.open({ 
                    type: 2,
                    title: "账户开户",
                    maxmin: true,
                    shadeClose: false, //点击遮罩关闭层
                    area : ['500px' , '750px'],
                    content: '../company/open_acc.htm?comnumId='+comnumId +'&comnumNum=' + comnumNum,
                    success: function(layero, index){
                    }
                }); 
        	}
        }
    });
  //账户查询
    $("#accountsearch").click(function () {
    	var selectContent = $table.bootstrapTable('getSelections')[0];
        if(typeof(selectContent) == 'undefined') {
        	layer.msg('请选择一条数据!');
            return false;
        }else{
        	var comnumId = selectContent.comnumId;
        	var comnumNum = selectContent.comnumNum;
        	var comnumAccountNo = selectContent.comnumAccountNo;
        	var comnumIsDelete = selectContent.comnumIsDelete;
        	if(comnumAccountNo== ''||comnumAccountNo== null||comnumAccountNo== undefined){
        		layer.msg('还没有账户号');
        	}
        	else{
        		var index = layer.open({ 
                    type: 2,
                    title: "账户查询",
                    maxmin: true,
                    shadeClose: false, //点击遮罩关闭层
                    area : ['500px' , '750px'],
                    content: '../company/acc_query.htm?comnumAccountNo='+comnumAccountNo,
                    success: function(layero, index){
                    }
                }); 
        	}
        }
    });
  //余额查询
    $("#balancesearch").click(function () {
    	var selectContent = $table.bootstrapTable('getSelections')[0];
        if(typeof(selectContent) == 'undefined') {
        	layer.msg('请选择一条数据!');
            return false;
        }else{
        	var comnumId = selectContent.comnumId;
        	var comnumNum = selectContent.comnumNum;
        	var comnumAccountNo = selectContent.comnumAccountNo;
        	var comnumIsDelete = selectContent.comnumIsDelete;
        	if(comnumAccountNo== ''||comnumAccountNo== null||comnumAccountNo== undefined){
        		layer.msg('还没有账户号');
        	}
        	else{
        		var index = layer.open({ 
                    type: 2,
                    title: "余额查询",
                    maxmin: true,
                    shadeClose: false, //点击遮罩关闭层
                    area : ['500px' , '750px'],
                    content: '../company/com_balance.htm?comnumAccountNo='+comnumAccountNo,
                    success: function(layero, index){
                    }
                }); 
        	}
        }
    });
    //账户变更
	$("#accountupdate").click(function () {
		var selectContent = $table.bootstrapTable('getSelections')[0];
	    if(typeof(selectContent) == 'undefined') {
	    	layer.msg('请选择一条数据!');
	        return false;
	    }else{
	    	var comnumId = selectContent.comnumId;
	    	var comnumNum = selectContent.comnumNum;
	    	var comnumIsDelete = selectContent.comnumIsDelete;
	    	if(comnumIsDelete == '1'){
	    		layer.msg('禁用状态不能变更账户');
	    	}else{
	    		var index = layer.open({ 
	                type: 2,
	                title: "账户变更",
	                maxmin: true,
	                shadeClose: false, //点击遮罩关闭层
	                area : ['500px' , '750px'],
	                content: '../company/acc_modify.htm?comnumId='+comnumId +'&comnumNum=' + comnumNum,
	                success: function(layero, index){
	                }
	            }); 
	    	}
	    }
	});	
});

function initTable() {
	$table.bootstrapTable({
        dataType: "json",
        showColumns: true,
        singleSelect : true,
        pagination: true, //分页
        striped: true,
        cache: false,	//是否使用缓存，默认是true
        pageSize: 10,
        pageNumber: 1,
        queryParamsType: 'limit',
        smartDisplay: true,
        sidePagination: "server",
        method: "post",	//使用post提交
        queryParams: queryParams,
        url: '../company/getCompanyPage.htm',
        contentType: 'application/x-www-form-urlencoded;charset=UTF-8',
        columns: [ 
			{
				title: "序号",
			    align: 'center',
			    formatter:function(value,row,index){
			    	return index + 1;
			    }
			},
			{
                checkbox: true,
                align: 'center',
                valign: 'middle'
            },
            /*{
                title: "企业编号",
                field: "comnumNum",
                align: 'center',
                valign: 'middle'
            },*/
            {
                title: "企业名称",
                field: "comnumName",
                align: 'center',
            	formatter: function (value, row, index) {
                    if(row.comnumName != null){
                 	   return '<a type="button" onclick="branchComInfo(\'' + row.comnumId  + '\',\'' + row.comnumName + '\')">'+row.comnumName+'</a>';
                    }
                    return '-';
                 }

            },
            {
                title: "企业地址",
                field: "comnumAddress",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "执照编号",
                field: "comnumLicenceCode",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "企业联系方式",
                field: "comnumPhone",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "银行卡号",
                field: "comnumBankCard",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "开户人",
                field: "comnumBankHolder",
                align: 'center',
                valign: 'middle'
            },
            {
            	title: "身份证号",
            	field: "comnumIdCard",
            	align: "center",
            	valign: "middle"
            },
            {
                title: "开户行",
                field: "comnumBankName",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "账户号",
                field: "comnumAccountNo",
                align: 'center',
                valign: 'middle',
            },
            {
                title: "插入时间",
                field: "comnumInsTime",
                align: 'center',
                valign: 'middle'
            },
            {
            	title: "账户类型",
            	field: "comnumType",
            	align: "center",
            	formatter: function(value, row, index){
            		var type = "";
            		if(row.comnumType == '00'){
            			type = '<span style="color:#6A5ACD;">对私</span>';
            		}else if(row.comnumType == '01'){
            			type = '<span style="color:#DA70D6;">对公</span>';
            		}else{
            			type = '-';
            		}
            		return type;
            	}
            },
            {
                title: "是否停运",
                field: "comnumIsDelete",
                align: 'center',
                formatter: function(value, row, index){
            		var stop = "";
            		if(row.comnumIsDelete == 1){
            			stop = '<span style="color:#ed5565;">已停运</span>';
            		}else if(row.comnumIsDelete == 0){
            			stop ='<span style="color:#23c6c8;">未停运</span>';
            		}
            		return stop;
            	}
            },
            {
                title: "操作",
                align: 'center',
                formatter: function (value, row, index) {
                    var e = '<button class="btn btn-xs btn-info" type="button" onclick="initCompanyEdit(\'' + row.comnumId + '\')"><i class="fa fa-edit"></i> 编辑</button> ';
                    var d = '';
                    var f = '';
                    if (row.comnumIsDelete == 0){
                        d = '<button class="btn btn-xs btn-danger" type="button" onclick="deleteCompany(\'' + row.comnumId + '\',\'' + row.comnumName + '\',\'' + 1 + '\',\'' + row.comnumNum + '\')"><i class="fa fa-trash-o"></i> 禁用 </button> ';
                    }else if(row.comnumIsDelete == 1){
                        d = '<button class="btn btn-xs btn-warning" type="button" onclick="deleteCompany(\'' + row.comnumId + '\',\'' + row.comnumName + '\',\'' + 0 + '\',\'' + row.comnumNum + '\')"><i class="fa fa-trash-o"></i> 启用 </button> ';
                    }
                    if(!isEmpty(row.comnumPhone) && !isEmpty(row.comnumBankCard) && !isEmpty(row.comnumBankName) && !isEmpty(row.comnumBankHolder) && !isEmpty(row.comnumIdCard) && isEmpty(row.comnumAccountNo)){
                    	f = '<button class="btn btn-xs btn-info" type="button" onclick="temporaryOpenAcc(\'' + row.comnumPhone + '\',\'' + row.comnumBankCard + '\',\'' + row.comnumBankName + '\',\'' + row.comnumBankHolder + '\',\'' + row.comnumId + '\',\'' + row.comnumId + '\',\'' + row.comnumNum +'\')"><i class="fa fa-trash-o"></i>开户</button> ';
                    }
                    return e + d + f;
                }
            }
        ]
    });
}

//分页参数，条件查询时的参数
function queryParams(queryParamsType){
	var temp = {
		limit: queryParamsType.limit,   //页面大小
		offset: queryParamsType.offset,  //页码
		comnumName: $.trim($('#comnumName').val()),
		comnumPhone: $.trim($('#comnumPhone').val()),
	};
	return temp;
}

//编辑公司信息
function initCompanyEdit(id){
    var title;
    if(id){
        title = '编辑企业';
    }else{
        title = '新增企业';
    }
    var index = layer.open({
        type: 2,
        title: title,
        maxmin: true,
        shadeClose: false, //点击遮罩关闭层
        area : ['500px' , '750px'],
        content: '../company/edit_company.htm?id='+id,
        success: function(layero, index){
        }
    });
    layer.full(index);
}

//设置公司状态
function deleteCompany(companyId, companyName, isDelete, companyNum) {
    layer.confirm('你确定要修改【' + companyName + '】企业状态吗？', {
        btn: ['修改', '取消'] //按钮
    }, function () {
        $.ajax({
            url: "../company/modifyCompanyDisable.htm",
            cache: false,
            type: "post",
            dataType: "json",
            data: {'companyId': companyId,'isDelete':isDelete, 'companyNum':companyNum},
            success: function (result) {
                if (result.status == 0) {
                    layer.msg(result.message, {icon: 1});
                    $table.bootstrapTable('refresh', {url: '../company/getCompanyPage.htm'});
                    return;
                }
                layer.msg(result.message, {icon: 2});
            }
        });
    }, function () {
    });
}

//绑卡操作
function bindingBankCard(companyId){
	 $.ajax({
	    url: "../company/bindingBankCard.htm",
	    cache: false,
	    type: "post",
	    dataType: "json",
	    data: {'companyId': companyId},
	    success: function (result) {
	        if (result.status == 0) {
	        	var cardNo = result.data.cardNo;	//银行卡号
	        	var merchant_no = result.data.merchant_no;	//商户号
	        	var version = result.data.version;	//版本
	        	var sign = result.data.sign;	//签名
	            //下面请求URL，进入绑定银行卡的iframe窗
	        	layer.open({
	  		      type: 2,
	  		      title: '绑定银行信用卡页面',
	  		      shadeClose: true,
	  		      shade: false,
	  		      maxmin: true, //开启最大化最小化按钮
	  		      area: ['893px', '700px'],
	  		      content: bindingBankURL + '?merchant_no=' + merchant_no +'&cardNo=' + cardNo + '&version='+ version +'&sign='+ sign
	  			});
	        } else {
	            layer.msg('数据错误，暂不能绑卡！', {icon: 2});
	            closeForm();
	        }
	    }
	 });
}

//comnum的下拉列表  追加到option中
function addOptions(data, selectId){
	for(var i=0; i<data.length; i++){
		$(selectId).append('<option value="' + data[i].comnumNum + '">' + data[i].comnumName + '</option>');
	}
}

//分公司详细信息
function branchComInfo(comnumId, comnumName){
	var index = layer.open({
	      type: 2,
	      title: '【' + comnumName + '】分公司信息',
	      shadeClose: false,
	      shade: false,
	      maxmin: true, //开启最大化最小化按钮
	      area: ['1300px', '800px'],
	      content: '../company/branch_company_list.htm?id='+comnumId,
		});
//	layer.full(index);
}

//临时开户按钮
function temporaryOpenAcc(comnumPhone,comnumBankCard,comnumBankName,comnumBankHolder,comnumIdCard,comnumId,comnumNum){
	$.ajax({
        url: "../company/temporaryOpenAcc.htm",
        cache: false,
        type: "post",
        dataType: "json",
        data: {'comnumPhone': comnumPhone,'comnumBankCard':comnumBankCard, 'comnumBankName':comnumBankName, 
        						'comnumBankHolder':comnumBankHolder, 'comnumIdCard':comnumIdCard, 'comnumId':comnumId, 'comnumNum':comnumNum},
        success: function (result) {
            if (result.status == 0) {
                layer.msg(result.message, {icon: 1});
                $table.bootstrapTable('refresh', {url: '../company/getCompanyPage.htm'});
                return;
            }
            layer.msg(result.message, {icon: 2});
        }
    });
}

//判断字符是否为空的方法
function isEmpty(obj){
	if(typeof obj == 'undefined' || obj == null || obj == ''){
		return true;
	}else{
		return false;
	}
}
